--
-- Author: Your Name
-- Date: 2014-12-06 11:07:27
--
local ChatItem = class("ChatItem",function() return display.newNode() end)

local World_Tag      = 1
local Monster_Tag    = 2
local Material_Tag   = 3

function ChatItem:ctor(target)

	self.table    = target

end

function ChatItem:refreshItem(data)  

	self:removeAllChildren()
	self.src_uid = data.src_uid


	local idx = data.idx

	if data.src_uid == DataSystem.userData:getSelfInfo().uid then
		self.leftOrRight = 1
	else
		self.leftOrRight = 0
	end

	--self.leftOrRight = idx%2
	if data.src_uid == ChatSystem.sysUid then 
		--data.src_nick = WordManager:getWord("ChatLayer_006")
    	local htmlText = "<font size=\"25\" color=\"#E78651\">".."【".."</font><font size=\"25\" color=\"#DBC651\">"..data.src_nick.."<font size=\"25\" color=\"#E78651\">".."】".."</font></font><font size=\"25\" color=\"#B762C0\">"..data.msg.."</font>"

		local node = getRichText(htmlText, ChatSystem.sysChatWidth,nil)     --.....

	    local height = self.table._displayingLabelHeightList[data.idx][1]
		node:setPosition((display.size.width-ChatSystem.sysChatWidth)*0.5 ,height - 30) 

		node:addTo(self)
	else
		local headIconPosX 
		local userNickAnch
		local userNickPosX


		if self.leftOrRight ==0 then

			headIconPosX = 40

			userNickAnch = CCPoint(0,0.5)-- ui.TEXT_ALIGN_LEFT
			userNickPosX = 73


		elseif self.leftOrRight ==1 then

			headIconPosX = display.width - 40

			userNickAnch = CCPoint(1,0.5)-- ui.TEXT_ALIGN_RIGHT
			userNickPosX = display.width - 73


		end


		local height = self.table._displayingLabelHeightList[data.idx][1]
		local row = self.table._displayingLabelHeightList[data.idx][2]
		self.headIconNode = CCNode:create()
		self.headIconNode:setAnchorPoint(CCPoint(0.5,0.5))
		self.headIconNode:setScale(0.8)
		
		local headIconspr1 = CCSprite:createWithSpriteFrameName("bg_img_76.png")
		headIconspr1:addTo(self.headIconNode)

		local headId = data.head_id

		local headPath =require("app.scenes.common.component.HeadIcon"):getHeadIconPath(headId)
        HeadIcon = require("app.scenes.common.component.HeadIcon"):createCircleHeadIcon(headPath,1)
        
        HeadIcon:addTo(self.headIconNode)
          --在这加头像 不用opentouch



		self.headIconNode:addTo(self):pos(headIconPosX,height + 50)



		local nickColor
		if tonumber(DataSystem.userData:getSelfInfo().uid) == data.src_uid then
			nickColor = ccc3(218,198,81)
		else
			nickColor = ccc3(82,229,210)
		end
		self.userNick = text.newTTFLabelWithOutline(data.src_nick, nickColor, text.color4, 25)


		self.userNick:setAnchorPoint(userNickAnch)
		self.userNick:addTo(self):pos(userNickPosX,height + 45)




		self.headIconNode:setTouchEnabled(true)
		self.headIconNode:setTouchSwallowEnabled(false)
   		self.headIconNode:addNodeEventListener(cc.NODE_TOUCH_EVENT, 
	   		function(event)
				if event.name == "began"  then
					self.table.isScroll = false
			    elseif event.name == "ended" and self.table.isScroll == false then 

			        SoundManager:playMainSceneSound("button")
			       	self:pop_callBack()
			    end
				return true
	   		end) 


   		self.userNick:setTouchEnabled(true)
   		self.userNick:setTouchSwallowEnabled(false)
   		self.userNick:addNodeEventListener(cc.NODE_TOUCH_EVENT, 
	   		function(event)
				if event.name == "began"  then
					self.table.isScroll = false
			    elseif event.name == "ended"  and self.table.isScroll == false then 
			        SoundManager:playMainSceneSound("button")
			       	self:pop_callBack()
			    end
				return true
	   		end)



		local labelNode = CCNode:create()
		labelNode:addTo(self)

        local label
        local bgWidth
   		if row==1 then
   			label = ui.newTTFLabel({text = data.msg,color = ccc3(77,48,17),size = 25,font = "Helvetica-Bold"})
			label:setAnchorPoint(display.ANCHOR_POINTS[display.LEFT_BOTTOM])
   			bgWidth = label:getBoundingBox().size.width + 20
   		else
	   		local htmlText = "<font size=\"25\" color=\"#4D3011\">"..data.msg.."</font>"
			label = getRichText(htmlText, ChatSystem.userChatWidth)     --.....
			bgWidth = ChatSystem.userChatWidth + 20
   		end

		local bg = CCScale9Sprite:createWithSpriteFrame(CCSpriteFrameCache:sharedSpriteFrameCache():spriteFrameByName("bg_img_131.png"));
        bg:setInsetLeft(20);
        bg:setInsetTop(10);
        bg:setInsetRight(10);
        bg:setInsetBottom(10);
		bg:setPreferredSize(CCSizeMake(bgWidth,height+20))
		
		bg:addTo(labelNode)
		label:addTo(labelNode)

		if self.leftOrRight ==0 then
			labelNode:setAnchorPoint(CCPoint(0,0))
			labelNode:setPosition(CCPoint(68,8))

			bg:setScaleX(1)
			bg:setAnchorPoint(CCPoint(0,0))
        	bg:setPosition( CCPoint(0,0))

        	label:setPosition( CCPoint(11,height-23))


		elseif self.leftOrRight ==1 then
			labelNode:setAnchorPoint(CCPoint(1,0))
			labelNode:setPosition(CCPoint(display.width-68,8))

			bg:setScaleX(-1)
			bg:setAnchorPoint(CCPoint(0,0))
        	bg:setPosition( CCPoint(0,0))

        	label:setPosition( CCPoint(11 - bgWidth,height-23))


		end

	end

end


function ChatItem:pop_callBack()

	local showType = {}
	showType.name = "Friend"
	local pop = require("app.scenes.main.layers.pop.HeadIconPopBox").new(self.table,self.src_uid,showType)
	SceneManager:addDisplayToCurrentScene(pop);

end


function ChatItem:clearNode(obj)   --清除一个节点 必须删除对他的监听
end

function ChatItem:resetItem() --清空item 
end

return ChatItem